<template>
  <div class="login">
    <div class="login-box">
      <!--头像区域 -->
      <div class="login-box-logo">
        <img src="../assets/logo2.png" alt="">
      </div>
      <!-- 登陆表单区域 -->
      <el-form :model="loginForm" :rules="loginFormRules" ref="loginFormRef" label-width="0px" class="login-form">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="el-icon-user-solid" ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" prefix-icon="el-icon-s-cooperation"></el-input>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登陆</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data () {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetLoginForm () {
      // console.log(this)
      this.$refs.loginFormRef.resetFields()
    },
    login () {
      this.$refs.loginFormRef.validate(valid => {
        if (!valid) return
        this.$http.login(this.loginForm.username, this.loginForm.password)
          .then(resp => {
            // console.log(resp)
            if (resp.data.meta.status !== 200) {
              this.$message({
                message: '登陆失败',
                type: 'error',
                center: true
              })
            } else {
              this.$message.success({ message: '登陆成功', center: true })
              window.sessionStorage.setItem('token', resp.data.data.token)
              this.$router.push('/home')
            }
          })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  .login {
    width: 100%;
    height: 100%;
    background: url('../assets/bj.png') no-repeat center center;
    background-size: cover;
    &-box {
      width: 450px;
      height: 300px;
      background-color: #9EC9DA;
      // background-color: #fff;
      // background-color: #ddd;
      position: absolute;
      top: 0;
      bottom: 0;
      margin: 150px auto;
      // right: 77px;
      right: 0;
      &-logo {
        height: 130px;
        width: 130px;
        border: 2px solid #fff;
        border-radius: 50%;
        padding: 10px;
        box-shadow: 0 0 10px #ddd;
        position: absolute;
        left: 50%;
        transform: translate(-50%, -50%);
        img {
          width: 130px;
          height: 71px;
          position: absolute;
          top: 0;
          bottom: 0;
          margin: 35px auto;
          border-radius: 50%;
        }
      }
      .login-form {
        position: absolute;
        bottom: 0;
        width: 100%;
        padding: 0 30px;
        box-sizing: border-box;
        .btns {
        display: flex;
        justify-content: flex-end;
      }
      }
    }
  }
</style>
